package quartz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.*;
import quartz.entity.JobEntity;
import quartz.service.ScheduleJobService;

@RestController
public class ScheduleJobController implements ApplicationRunner {
    @Autowired
    private ScheduleJobService scheduleJobService;

    @Override
    public void run(ApplicationArguments args) {
        scheduleJobService.refreshAll();
    }

    @RequestMapping("/refresh-all")
    public void refreshAll() {
        scheduleJobService.refreshAll();
    }

    @RequestMapping("/add")
    public void add() {
        JobEntity jobEntity = new JobEntity();
        jobEntity.setClassPath("quartz.job.MyJob");
        jobEntity.setTargetMethod("test");
        jobEntity.setJobName("my job");
        jobEntity.setJobGroup("quartz job");
        jobEntity.setTriggerName("test method");
        jobEntity.setTriggerGroup("quartz trigger");
        jobEntity.setCron("*/5 * * * * ?");
        jobEntity.setIsActive(true);
        jobEntity.setDescription("test quartz");
        scheduleJobService.addJob(jobEntity);
    }

    @RequestMapping("/pause/{jobEntityId}")
    public void pause(@PathVariable String jobEntityId) {
        scheduleJobService.pauseJob(jobEntityId);
    }

    @RequestMapping("/execute/{jobEntityId}")
    public void execute(@PathVariable String jobEntityId) {
        scheduleJobService.executeJob(jobEntityId);
    }

    @RequestMapping("/resume/{jobEntityId}")
    public void resume(@PathVariable String jobEntityId) {
        scheduleJobService.resumeJob(jobEntityId);
    }

    @PostMapping("/update")
    public void update(@RequestBody JobEntity jobEntity) {
        scheduleJobService.updateJob(jobEntity);
    }

    @RequestMapping("/disable/{jobEntityId}")
    public void disable(@PathVariable String jobEntityId) {
        scheduleJobService.disableJob(jobEntityId);
    }

    @RequestMapping("/activate/{jobEntityId}")
    public void activate(@PathVariable String jobEntityId) {
        scheduleJobService.activateJob(jobEntityId);
    }

    @RequestMapping("/delete/{jobEntityId}")
    public void delete(@PathVariable String jobEntityId) {
        scheduleJobService.deleteJob(jobEntityId);
    }
}
